import {Buffer} from "buffer";
import {GetRequestNormal} from "./GetRequestNormal";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {GetRequestNormalList} from "./GetRequestNormalList";
import {GetRequestRecord} from "./GetRequestRecord";
import {GetRequestRecordList} from "./GetRequestRecordList";
import {GetRequestNext} from "./GetRequestNext";


enum Choice {
    _ERR_NONE_SELECTED = -1,
    GET_REQUEST_NORMAL = 1,
    GET_REQUEST_NORMAL_LIST = 2,
    GET_REQUEST_RECORD = 3,
    GET_REQUEST_RECORD_LIST = 4,
    GET_REQUEST_NEXT = 5,
}

export class Get_Request implements AxdrType {

    dataCode: Buffer | null = null;

    choice: Choice = Choice._ERR_NONE_SELECTED;

    getRequestNormal: GetRequestNormal | null = null;

    getRequestNormalList : GetRequestNormalList | null = null;

    getRequestRecord : GetRequestRecord | null = null;

    getRequestRecordList : GetRequestRecordList | null = null;

    getRequestNext : GetRequestNext | null = null;

    constructor() {

    }

    set_dataCode(dataCode: Buffer) {
        this.dataCode = dataCode;
    }

    setGetRequestNormal(getRequestNormal: GetRequestNormal) {
        this.resetChoices();
        this.choice = Choice.GET_REQUEST_NORMAL;
        this.getRequestNormal = getRequestNormal;
    }

    setGetRequestNormalList(getRequestNormalList : GetRequestNormalList){
        this.resetChoices();
        this.choice = Choice.GET_REQUEST_NORMAL_LIST;
        this.getRequestNormalList = getRequestNormalList;
    }

    setGetRequestRecord(getRequestRecord : GetRequestRecord){
        this.resetChoices();
        this.choice = Choice.GET_REQUEST_RECORD;
        this.getRequestRecord = getRequestRecord;
    }

    setGetRequestRecordList(getRequestRecordList : GetRequestRecordList){
        this.resetChoices();
        this.choice = Choice.GET_REQUEST_RECORD_LIST;
        this.getRequestRecordList = getRequestRecordList;
    }

    setGetRequestNext(getRequestNext : GetRequestNext){
        this.resetChoices();
        this.choice = Choice.GET_REQUEST_NEXT;
        this.getRequestNext = getRequestNext;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            this.getRequestNormal = new GetRequestNormal();
            codeLength += this.getRequestNormal.decode(input);
            return codeLength;
        }else if(this.choice == Choice.GET_REQUEST_NORMAL_LIST){
            this.getRequestNormalList = new GetRequestNormalList();
            codeLength += this.getRequestNormalList.decode(input);
            return codeLength;
        }else if(this.choice == Choice.GET_REQUEST_RECORD){
            this.getRequestRecord = new GetRequestRecord();
            codeLength += this.getRequestRecord.decode(input);
            return codeLength;
        }else if(this.choice == Choice.GET_REQUEST_RECORD_LIST){
            this.getRequestRecordList = new GetRequestRecordList();
            codeLength += this.getRequestRecordList.decode(input);
            return codeLength;
        }else if(this.choice == Choice.GET_REQUEST_NEXT){
            this.getRequestNext = new GetRequestNext();
            codeLength += this.getRequestNext.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.")
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.")
        }
        let codeLength = 0;
        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            if (this.getRequestNormal != null) {
                codeLength += this.getRequestNormal.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Error("getRequestNormal is null");
            }
        }else if(this.choice == Choice.GET_REQUEST_NORMAL_LIST){
            if (this.getRequestNormalList != null) {
                codeLength += this.getRequestNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getRequestNormalList is null");
            }
        }else if(this.choice == Choice.GET_REQUEST_RECORD){
            if(this.getRequestRecord != null){
                codeLength += this.getRequestRecord.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(3);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getRequestRecord is null");
            }
        }else if(this.choice == Choice.GET_REQUEST_RECORD_LIST){
            if(this.getRequestRecordList != null){
                codeLength += this.getRequestRecordList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(4);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getRequestRecordList is null");
            }
        }else if(this.choice == Choice.GET_REQUEST_NEXT){
            if(this.getRequestNext != null){
                codeLength += this.getRequestNext.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(5);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("getRequestNext is null");
            }
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.getRequestNormal = null;
        this.getRequestNormalList = null;
        this.getRequestRecord = null
        this.getRequestRecordList = null;
        this.getRequestNext = null;
    }

    toString(): string {
        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            if (this.getRequestNormal != null) {
                return "choice: {getRequestNormal: " + this.getRequestNormal + "}";
            } else {
                return "choice is getRequestNormal but getRequestNormal is null";
            }
        }else if(this.choice == Choice.GET_REQUEST_NORMAL_LIST){
            if(this.getRequestNormalList != null){
                return "choice: {getRequestNormalList: " + this.getRequestNormalList + "}";
            }else{
                return "choice is getRequestNormalList but getRequestNormalList is null";
            }
        }else if(this.choice == Choice.GET_REQUEST_RECORD){
            if(this.getRequestRecord != null){
                return "choice: {getRequestRecord: " + this.getRequestRecord + "}";
            }else{
                return "choice is getRequestRecord but getRequestRecord is null";
            }
        }else if(this.choice == Choice.GET_REQUEST_RECORD_LIST){
            if(this.getRequestRecordList != null){
                return "choice: {getRequestRecordList: " + this.getRequestRecordList + "}";
            }else{
                return "choice is getRequestRecordList but getRequestRecordList is null";
            }
        }else if(this.choice == Choice.GET_REQUEST_NEXT){
            if(this.getRequestNext != null){
                return "choice: {getRequestNext: " + this.getRequestNext + "}";
            }else{
                return "choice is getRequestNext but getRequestNext is null";
            }
        } else {
            return "unknown";
        }
    }

}
